<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BSEntity
 *
 * @author jianfeng
 */
class BWEntityWithGroup extends BEntityWithGroup {

    public $attributes = array();

    public function getModelClass($data = NULL) {
        $class = get_class($this);
        $mClass = preg_replace("/^BW/", 'BWM', $class);
        return $mClass;
    }

    public function getModelClassAsGroupMember($data = NULL) {
        return $this->getModelClass() . 'GroupMember';
    }

    public function getPropertyToColumnMapList($data = NULL) {
        //by default, use property name as column name       

        $list['amount'] = 'amount';
        $list['name'] = 'name';
        $list['description'] = 'description';
        $list['spaceId'] = 'space_id';
        $list['appId'] = 'app_id';
        $list['creatorId'] = 'creator_id';
        $list['ownerId'] = 'owner_id';
        $list['roleId'] = 'role_id';
        $list['status'] = 'status';
        $list['createTime'] = 'create_time';
        $list['unitPrice'] = 'unit_price';
        $list['priority'] = 'priority';
        $list['timeslotId'] = 'timeslot_id';
        $list['resourceId'] = 'resource_id';
        return $list;
    }

    public function initBeforeLoad($data = NULL) {
        
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $modelClass = $this->getModelClass();
        $repository->setModel($modelClass);
        return $repository;
    }

    public function save($context = null) {
        $repository = $this->getRepository();
        return $repository->saveEntity($context, $this);
    }

    public function load($data = NULL, $context = NULL) {
        //parent::load($data, $context);
        $this->getRepository()->loadEntity($data, $this);
    }

    public function addAttribute($name, $value, $data = array()) {
        if (!is_array($data)) {
            $data = array();
        }
        if (!isset($data['attributeClass']))
            $data['attributeClass'] = 'BWAttribute';
        return parent::addAttribute($name, $value, $data);
    }

    public function getAttribute($name, $data = array()) {
        if (!is_array($data)) {
            $data = array();
            //$data['attributeClass'] = 'BWAttribute';
        }
        if (!isset($data['attributeClass']))
            $data['attributeClass'] = 'BWAttribute';
        return parent::getAttribute($name, $data);
    }

    public function getAttributes($data = array()) {
        if (!is_array($data))
            $data = array();
        if (!isset($data['attributeClass']))
            $data['attributeClass'] = 'BWAttribute';
        return parent::getAttributes($data);
    }

    public function removeAttribute($name, $data = array()) {
        if (!is_array($data))
            $data = array();
        if (!isset($data['attributeClass']))
            $data['attributeClass'] = 'BWAttribute';
        return parent::removeAttribute($name, $data);
    }

}
